<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.12"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>emp-toolkit: /Users/wangxiao/git/emp-toolkit/emp-tool/io/file_io_channel.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo-full-doxygen.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">emp-toolkit
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.12 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('file__io__channel_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">file_io_channel.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="file__io__channel_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#ifndef FILE_IO_CHANNEL_H__</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#define FILE_IO_CHANNEL_H__</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;</div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &lt;string.h&gt;</span></div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor">#include &lt;arpa/inet.h&gt;</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="preprocessor">#include &lt;sys/types.h&gt;</span></div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#include &lt;netinet/in.h&gt;</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="preprocessor">#include &lt;sys/socket.h&gt;</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="preprocessor">#include &quot;<a class="code" href="io__channel_8h.html">io_channel.h</a>&quot;</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="keyword">using namespace </span><a class="code" href="namespacestd.html">std</a>;</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno"><a class="line" href="class_file_i_o.html">   21</a></span>&#160;<span class="keyword">class </span><a class="code" href="class_file_i_o.html">FileIO</a>: <span class="keyword">public</span> <a class="code" href="class_i_o_channel.html">IOChannel</a>&lt;FileIO&gt; { <span class="keyword">public</span>:</div><div class="line"><a name="l00022"></a><span class="lineno"><a class="line" href="class_file_i_o.html#a0223eb92f09bdd9e367ecd468c09c588">   22</a></span>&#160;    uint64_t bytes_sent = 0;</div><div class="line"><a name="l00023"></a><span class="lineno"><a class="line" href="class_file_i_o.html#a0ea1e4a756840d1c23ca7a6de650a9dd">   23</a></span>&#160;    <span class="keywordtype">int</span> mysocket = -1;</div><div class="line"><a name="l00024"></a><span class="lineno"><a class="line" href="class_file_i_o.html#ac0a10f239f47ef507ce1eb56427cea75">   24</a></span>&#160;    FILE * stream = <span class="keyword">nullptr</span>;</div><div class="line"><a name="l00025"></a><span class="lineno"><a class="line" href="class_file_i_o.html#a5d488232cec5dfc12a2938519a5348cf">   25</a></span>&#160;    <span class="keywordtype">char</span> * buffer = <span class="keyword">nullptr</span>;</div><div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="class_file_i_o.html#a526088af02f69496a0bcdb53059f948a">   26</a></span>&#160;    <a class="code" href="class_file_i_o.html#a526088af02f69496a0bcdb53059f948a">FileIO</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> * file, <span class="keywordtype">bool</span> read) {</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;        <span class="keywordflow">if</span> (read)</div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;            stream = fopen(file, <span class="stringliteral">&quot;rb+&quot;</span>);</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;        <span class="keywordflow">else</span></div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;            stream = fopen(file, <span class="stringliteral">&quot;wb+&quot;</span>);</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;        buffer = <span class="keyword">new</span> <span class="keywordtype">char</span>[<a class="code" href="config_8h.html#a09dbbd73a84cf772b421c6024b65b1fd">FILE_BUFFER_SIZE</a>];</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;        memset(buffer, 0, <a class="code" href="config_8h.html#a09dbbd73a84cf772b421c6024b65b1fd">FILE_BUFFER_SIZE</a>);</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;        setvbuf(stream, buffer, _IOFBF, <a class="code" href="config_8h.html#a09dbbd73a84cf772b421c6024b65b1fd">FILE_BUFFER_SIZE</a>);</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;    }</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;</div><div class="line"><a name="l00036"></a><span class="lineno"><a class="line" href="class_file_i_o.html#adc3caa8f1e5d76274d8ffb8b5c17288b">   36</a></span>&#160;    <a class="code" href="class_file_i_o.html#adc3caa8f1e5d76274d8ffb8b5c17288b">~FileIO</a>(){</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;        fflush(stream);</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;        fclose(stream);</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        <span class="keyword">delete</span>[] buffer;</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;    }</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;</div><div class="line"><a name="l00042"></a><span class="lineno"><a class="line" href="class_file_i_o.html#ab8fd3017da405deec3404c7b2c8bb4eb">   42</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_file_i_o.html#ab8fd3017da405deec3404c7b2c8bb4eb">flush</a>() {</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;        fflush(stream);</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;    }</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;</div><div class="line"><a name="l00046"></a><span class="lineno"><a class="line" href="class_file_i_o.html#afe4e161253f160edf2f0d9236f115a05">   46</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_file_i_o.html#afe4e161253f160edf2f0d9236f115a05">reset</a>() {</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;        rewind(stream);</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    }</div><div class="line"><a name="l00049"></a><span class="lineno"><a class="line" href="class_file_i_o.html#a38f8a7d43703ae2eb67c91232e81c29a">   49</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_file_i_o.html#a38f8a7d43703ae2eb67c91232e81c29a">send_data_impl</a>(<span class="keyword">const</span> <span class="keywordtype">void</span> * data, <span class="keywordtype">int</span> len) {</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;        bytes_sent += len;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;        <span class="keywordtype">int</span> sent = 0;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;        <span class="keywordflow">while</span>(sent &lt; len) {</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;            <span class="keywordtype">int</span> res = fwrite(sent+(<span class="keywordtype">char</span>*)data, 1, len-sent, stream);</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;            <span class="keywordflow">if</span> (res &gt;= 0)</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;                sent+=res;</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;            <span class="keywordflow">else</span></div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;                fprintf(stderr,<span class="stringliteral">&quot;error: file_send_data %d\n&quot;</span>, res);</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;        }</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;    }</div><div class="line"><a name="l00060"></a><span class="lineno"><a class="line" href="class_file_i_o.html#ae4a2fda2e928d28ec52c777945039ef3">   60</a></span>&#160;    <span class="keywordtype">void</span> <a class="code" href="class_file_i_o.html#ae4a2fda2e928d28ec52c777945039ef3">recv_data_impl</a>(<span class="keywordtype">void</span>  * data, <span class="keywordtype">int</span> len) {</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;        <span class="keywordtype">int</span> sent = 0;</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;        <span class="keywordflow">while</span>(sent &lt; len) {</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;            <span class="keywordtype">int</span> res = fread(sent+(<span class="keywordtype">char</span>*)data, 1, len-sent, stream);</div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;            <span class="keywordflow">if</span> (res &gt;= 0)</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;                sent+=res;</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;            <span class="keywordflow">else</span> </div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;                fprintf(stderr,<span class="stringliteral">&quot;error: file_recv_data %d\n&quot;</span>, res);</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;        }</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    }</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;};</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;<span class="preprocessor">#endif//FILE_IO_CHANNEL</span></div><div class="ttc" id="class_file_i_o_html_ae4a2fda2e928d28ec52c777945039ef3"><div class="ttname"><a href="class_file_i_o.html#ae4a2fda2e928d28ec52c777945039ef3">FileIO::recv_data_impl</a></div><div class="ttdeci">void recv_data_impl(void *data, int len)</div><div class="ttdef"><b>Definition:</b> file_io_channel.h:60</div></div>
<div class="ttc" id="class_file_i_o_html_a38f8a7d43703ae2eb67c91232e81c29a"><div class="ttname"><a href="class_file_i_o.html#a38f8a7d43703ae2eb67c91232e81c29a">FileIO::send_data_impl</a></div><div class="ttdeci">void send_data_impl(const void *data, int len)</div><div class="ttdef"><b>Definition:</b> file_io_channel.h:49</div></div>
<div class="ttc" id="io__channel_8h_html"><div class="ttname"><a href="io__channel_8h.html">io_channel.h</a></div></div>
<div class="ttc" id="namespacestd_html"><div class="ttname"><a href="namespacestd.html">std</a></div></div>
<div class="ttc" id="class_file_i_o_html_afe4e161253f160edf2f0d9236f115a05"><div class="ttname"><a href="class_file_i_o.html#afe4e161253f160edf2f0d9236f115a05">FileIO::reset</a></div><div class="ttdeci">void reset()</div><div class="ttdef"><b>Definition:</b> file_io_channel.h:46</div></div>
<div class="ttc" id="config_8h_html_a09dbbd73a84cf772b421c6024b65b1fd"><div class="ttname"><a href="config_8h.html#a09dbbd73a84cf772b421c6024b65b1fd">FILE_BUFFER_SIZE</a></div><div class="ttdeci">#define FILE_BUFFER_SIZE</div><div class="ttdef"><b>Definition:</b> config.h:8</div></div>
<div class="ttc" id="class_file_i_o_html_adc3caa8f1e5d76274d8ffb8b5c17288b"><div class="ttname"><a href="class_file_i_o.html#adc3caa8f1e5d76274d8ffb8b5c17288b">FileIO::~FileIO</a></div><div class="ttdeci">~FileIO()</div><div class="ttdef"><b>Definition:</b> file_io_channel.h:36</div></div>
<div class="ttc" id="class_file_i_o_html_a526088af02f69496a0bcdb53059f948a"><div class="ttname"><a href="class_file_i_o.html#a526088af02f69496a0bcdb53059f948a">FileIO::FileIO</a></div><div class="ttdeci">FileIO(const char *file, bool read)</div><div class="ttdef"><b>Definition:</b> file_io_channel.h:26</div></div>
<div class="ttc" id="class_i_o_channel_html"><div class="ttname"><a href="class_i_o_channel.html">IOChannel</a></div><div class="ttdef"><b>Definition:</b> io_channel.h:12</div></div>
<div class="ttc" id="class_file_i_o_html_ab8fd3017da405deec3404c7b2c8bb4eb"><div class="ttname"><a href="class_file_i_o.html#ab8fd3017da405deec3404c7b2c8bb4eb">FileIO::flush</a></div><div class="ttdeci">void flush()</div><div class="ttdef"><b>Definition:</b> file_io_channel.h:42</div></div>
<div class="ttc" id="class_file_i_o_html"><div class="ttname"><a href="class_file_i_o.html">FileIO</a></div><div class="ttdef"><b>Definition:</b> file_io_channel.h:21</div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_eec959d81771dc8b1a750b834e1341eb.html">emp-tool</a></li><li class="navelem"><a class="el" href="dir_6f7196b439314a1a73bb5510dd78598a.html">io</a></li><li class="navelem"><a class="el" href="file__io__channel_8h.html">file_io_channel.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.12 </li>
  </ul>
</div>
</body>
</html>
